package com.spzx.product.api.factory;

import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.domain.vo.CategoryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;

import java.util.List;

/**
 * 分类接口调用的降级处理类
 *  降级处理类开发：两种
 *      1.可以使用OpenFeign组件提供的org.springframework.cloud.openfeign.FallbackFactory接口开发降级处理类。
 *      2.可以使用Sentineel组件，提供降级处理类。
 *
 */

public class RemoteCategoryFallbackFactory implements FallbackFactory<RemoteCategoryService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteCategoryFallbackFactory.class);

    @Override
    public RemoteCategoryService create(Throwable throwable) {
        log.error("商品服务调用失败:{}", throwable.getMessage());
        return new RemoteCategoryService() {

            @Override
            public R<List<CategoryVo>> getOneCategory(String source) {
                return R.fail("获取全部一级分类失败:" + throwable.getMessage());
            }

            @Override
            public R<List<CategoryVo>> tree(String source) {
                return R.fail("获取全部三级分类失败:" + throwable.getMessage());
            }
        };
    }
}
